package org.lex.swing.tab;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class CloseTab extends JPanel implements Tab, CanClose, HasIcon, HasTitle, HasToolTip, TabbedPaneAccesser {
	private JTabbedPane tabPane = null;
	private CloseHeader header = null;
	private Component component = null;
	private CloseAction closeAction = CloseAction.CLOSE;

	public CloseTab(Component component) {
		this.component = component;
		this.header = new CloseHeader();
		this.header.getButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (CloseAction.CLOSE == closeAction)
					close();
			}
		});
	}

	public CloseAction getCloseAction() {
		return this.closeAction;
	}

	public void setCloseAction(CloseAction action) {
		this.closeAction = action;
	}

	public JButton getCloseButton() {
		return header.getButton();
	}

	// CanClose
	public void close() {
		if (null != this.tabPane)
			tabPane.remove(this.component);
	}

	// Tab
	public Component getComponent() {
		return component;
	}

	public Component getHeader() {
		return header;
	}

	// TabbedPaneAccesser
	public JTabbedPane getTabbedPane() {
		return this.tabPane;
	}

	public void setTabbedPane(JTabbedPane tabbedPane) {
		this.tabPane = tabbedPane;
	}

	// HasToolTip
	public String getToolTip() {
		return header.getToolTip();
	}

	public void setToolTip(String tip) {
		header.setToolTip(tip);
	}

	// HasIcon
	public Icon getIcon() {
		return header.getIcon();
	}

	public void setIcon(Icon icon) {
		header.setIcon(icon);
	}

	// HasTitle
	public String getTitle() {
		return header.getTitle();
	}

	public void setTitle(String title) {
		header.setTitle(title);
	}
}
